﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvvmTreeView;
using UIObjects.TreeViewInterfaces;
using TreeBuilder.Contracts;
using System.Windows.Controls;
using System.ComponentModel;
using System.Collections.Specialized;

namespace TreeBuilder.ViewModels
{
    [TreeNodeRootSource(typeof(TreeBuilderTreeView), "")]
    public class TreeBuilderTreeView : ITreeView
    {
        private TreeNodeCollection nodes;
        private IRootsCollection roots;
        private ITreeBuilderService treeBuilderService;
        private TreeView internalTreeView;
        public event BusyHandler Busy;
        public event StatusHandler OnStatus;
        public event RootNodesAvailableHandler RootNodesAvailable;

        public TreeBuilderTreeView(ITreeBuilderService providerService, IRootsCollection roots)
        {
            this.treeBuilderService = providerService;
            this.roots = roots;

            this.nodes = new TreeNodeCollection();

            roots.CollectionChanged += new NotifyCollectionChangedEventHandler(RootsCollectionChanged);
            roots.PropertyChanged += new PropertyChangedEventHandler(RootsPropertyChanged);

            RootsCollectionChanged(this, null);
        }

        public TreeView InternalTreeView
        {
            get
            {
                return internalTreeView;
            }

            set
            {
                internalTreeView = value;

                internalTreeView.SelectedItemChanged += (e, sender) =>
                {
                    var treeNode = (ITreeNode)internalTreeView.SelectedItem;

                    if (treeNode.SelectionStatus != null && OnStatus != null)
                    {
                        OnStatus(treeNode, treeNode.SelectionStatus);
                    }
                };
            }
        }

        public void SetBusy(ITreeNode treeNode, bool busy)
        {
            Busy(treeNode, busy);
        }

        private void RootsPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
        }

        private void RootsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            roots.AsQueryable().Cast<ITreeBuilderRoot>()
                .AsQueryable()
                .Select(r => new TreeBuilderRoot(treeBuilderService, this, r, null)).Where(d => !nodes.Any(n => ((ITreeNodeServerEntity)n.Tag).ID == ((ITreeNodeServerEntity)(d.Tag)).ID))
                .ToList()
                .ForEach(d => nodes.Add(d));
        }

        public System.Windows.Media.Color BackColor
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public System.Windows.Controls.Image BackgroundImage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool CheckBoxes
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public System.Windows.Media.Color ForeColor
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool HideSelection
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int ImageIndex
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string ImageKey
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public ImageList ImageList { get; set; }
        public ImageList StateImageList { get; set; }

        public int Indent
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int ItemHeight
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool LabelEdit
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public System.Windows.Media.Color LineColor
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        ////[TreeNodeChildSource(typeof(TreeBuilderTreeNodeBase), DragDropEffect.Copy, 0)]
        public TreeNodeCollection Nodes
        {
            get 
            {
                return nodes;
            }
        }

        public string PathSeparator
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool RightToLeftLayout
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool Scrollable
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int SelectedImageIndex
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string SelectedImageKey
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public ITreeNode SelectedNode
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool ShowLines
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool ShowNodeToolTips
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool ShowPlusMinus
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool ShowRootLines
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool Sorted
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Text
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public ITreeNode TopNode
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public System.Collections.IComparer TreeViewNodeSorter
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int VisibleCount
        {
            get { throw new NotImplementedException(); }
        }

        public event EventHandler BackgroundImageChanged;

        public event EventHandler BackgroundImageLayoutChanged;

        public event EventHandler PaddingChanged;

        public event EventHandler RightToLeftLayoutChanged;

        public event EventHandler TextChanged;

        public void BeginUpdate()
        {
            throw new NotImplementedException();
        }

        public void CollapseAll()
        {
            throw new NotImplementedException();
        }

        public void EndUpdate()
        {
            throw new NotImplementedException();
        }

        public void ExpandAll()
        {
            throw new NotImplementedException();
        }

        public ITreeNode GetNodeAt(System.Windows.Point pt)
        {
            throw new NotImplementedException();
        }

        public ITreeNode GetNodeAt(int x, int y)
        {
            throw new NotImplementedException();
        }

        public int GetNodeCount(bool includeSubTrees)
        {
            throw new NotImplementedException();
        }

        public void Sort()
        {
            throw new NotImplementedException();
        }
    }
}
